package com.sc.nft.controller;




import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.annotation.UserRecordTimes;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.CollectionAttentionOrCancelDTO;
import com.sc.nft.entity.dto.SecondCollectionConfigDTO;
import com.sc.nft.entity.dto.WhetherAttentionDTO;
import com.sc.nft.entity.vo.SecondCollectionDetailV3_7VO;
import com.sc.nft.entity.vo.WhetherAttentionVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.CollectionAttentionService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 藏品关注(CollectionAttention)表控制层
 *
 * @author 王伟成
 * @since 2024-03-06 13:58:24
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("collectionAttention")
@Api(tags = "藏品关注")
public class CollectionAttentionController {
    /**
     * 服务对象
     */
    private final CollectionAttentionService collectionAttentionService;
    private final RedisTemplate redisTemplate;
    private final Redisson redisson;

    @ApiOperation(value = "关注或取消关注", notes = "关注或取消关注", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/attentionOrCancel", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result attention(@RequestBody CollectionAttentionOrCancelDTO collectionAttentionOrCancelDTO) {
        UserInfo user = UserPool.getUser();
        if (!user.getIsSm()) {
            throw new GlobalRunTimeException("实名后才可以关注");
        }
        String key= StrUtil.format("{}{}", RedisKeyEnum.COLLECTION_ATTENTION,user.getId());
        RLock lock = redisson.getLock(key);
        try{
            boolean b = lock.tryLock(3, TimeUnit.SECONDS);
            if (!b) {
                throw new GlobalRunTimeException("请求频率过高");
            }
            collectionAttentionService.attentionOrCancel(user,collectionAttentionOrCancelDTO);
        }catch (Exception e){
           throw new GlobalRunTimeException("系统繁忙,请稍后再试");
        }finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }
        return Result.success();
    }

    @ApiOperation(value = "用户是否关注当前藏品", notes = "用户是否关注当前藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/whetherAttention", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<WhetherAttentionVO> whetherAttention(@RequestBody WhetherAttentionDTO whetherAttentionDTO) {
        UserInfo user = UserPool.getUser();
        return Result.success(collectionAttentionService.whetherAttention(user,whetherAttentionDTO));
    }

    @ApiOperation(value = "关注列表", notes = "关注列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/attentionList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserRecordTimes
    @UserLogin
    public PageResult<SecondCollectionConfigDTO> attentionList(@RequestBody PageRequest  pageRequest) {
        UserInfo user = UserPool.getUser();
        if (!user.getIsSm()) {
            throw new GlobalRunTimeException("实名后才可使用该功能");
        }
        return PageResult.success(collectionAttentionService.attentionList(user,pageRequest));
    }
}
